<?php

namespace App\Http\Requests;

use App\Enum\ShopType;
use App\Util\ApiResult;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class FindFloorApiRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $this->needsJson();
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'shop_type' => 'bail|required|in:'.ShopType::BOTTOM_SHOP.','.ShopType::OFFICE_BUILDING,
            'area' => 'bail|required|integer|min:1',
            'sizeStart' => 'bail|required|numeric',
            'sizeEnd' => 'bail|required|numeric',
            'rentStart' => 'bail|required|numeric',
            'rentEnd' => 'bail|required|numeric',
            'facilities' => 'required'
        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'shop_type.required' => '商铺类型必选',
            'shop_type.in' => '商铺类型不正确',
            'area.required' => '区域必选',
            'area.integer' => '区域只能是整数',
            'area.min' => '区域必须大于1',
            'sizeStart.required' => '面积范围必选',
            'sizeStart.numeric' => '面积范围只能是数字',
            'sizeEnd.required' => '面积范围必选',
            'sizeEnd.numeric' => '面积范围只能是数字',
            'rentStart.required' => '租金范围必选',
            'rentStart.numeric' => '租金范围只能是数字',
            'facilities.required' => '配套设施必选'
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function formatErrors(Validator $validator)
    {
        return ApiResult::failure( $validator->errors()->first() )->getResult();
    }
}
